package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TFinanceData;
import com.quwan.entity.TPreserveData;
import com.quwan.entity.TSpreadData;
import com.quwan.manager.controller.dto.BalanceLogDTO;
import com.quwan.manager.controller.dto.DashboardVo;
import com.quwan.manager.service.DataDashboardService;
import com.quwan.utils.DateUtils;
import com.quwan.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author quan
 * @date 2021-07-26 16:46
 */
@Api(tags = "后台数据报表")
@RestController
@RequestMapping("mgr/data")
@RequiredArgsConstructor
public class DataDashboardController extends BaseController {
    private final DataDashboardService dashboardService;
    private static final int DEF_EXP_SIZE = 1000;

    @ApiOperation(value = "首页数据汇总", notes = "publishOrderMemberCount——发单人数，companyCommission——公司盈利，rechargeVipCount——充值人数，memberCount——会员注册数量，consumerOrderMemberCount——下单人数（接单人）")
    @GetMapping("index")
    public ApiResult<Map<String, String>> indexDashboardData() {
        return ApiResult.success(dashboardService.getIndexDashboardData());
    }

    @ApiOperation(value = "首页趋势图", notes = "publishOrderMemberCount——发单人数，publishOrderMemberCount——公司盈利，rechargeVipCount——充值人数，memberCount——会员注册数量，consumerOrderMemberCount——下单人数（接单人）")
    @GetMapping("indexDashboard")
    public ApiResult<DashboardVo> indexDashboardData(@ApiParam("开始日期 2022-12-12") @RequestParam(required = false) String beginTime,
                                                     @ApiParam("结束日期 2022-12-12") @RequestParam(required = false) String endTime) {
        LocalDate begin = null;
        LocalDate end = null;
        if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime) ) {
            begin = DateUtils.localDateOfStr(beginTime);
            end = DateUtils.localDateOfStr(endTime);
        }
        return ApiResult.success(dashboardService.getIndexDashboardData(begin, end));
    }

    @ApiOperation("推广数据列表")
    @PostMapping("spread")
    public ApiResult<PageResult<TSpreadData>> spreadDataList(@RequestParam(defaultValue = "1", required = false) int page,
                                                             @RequestParam(defaultValue = "7", required = false) int size,
                                                             @ApiParam("开始时间") @RequestParam(required = false) String beginTime,
                                                             @ApiParam("结束时间") @RequestParam(required = false) String endTime,
                                                             @ApiParam("本周") @RequestParam(required = false) String curWeek,
                                                             @ApiParam("本月") @RequestParam(required = false) String curMonth) {
        PageResult<TSpreadData> res =
                dashboardService
                        .getSpreadDataList(page,
                                size,
                                beginTime,
                                endTime,
                                curWeek,
                                curMonth);
        return ApiResult.success(res);
    }

    @ApiOperation("财务数据列表")
    @PostMapping("finance")
    public ApiResult<PageResult<TFinanceData>> financeDataList(@RequestParam(defaultValue = "1", required = false) int page,
                                                               @RequestParam(defaultValue = "7", required = false) int size,
                                                               @ApiParam("开始时间") @RequestParam(required = false) String beginTime,
                                                               @ApiParam("结束时间") @RequestParam(required = false) String endTime,
                                                               @ApiParam("本周") @RequestParam(required = false) String curWeek,
                                                               @ApiParam("本月") @RequestParam(required = false) String curMonth) {
        PageResult<TFinanceData> res =
                dashboardService
                        .getFinanceDataList(
                                page,
                                size,
                                beginTime,
                                endTime,
                                curWeek,
                                curMonth);
        return ApiResult.success(res);
    }

    @ApiOperation("推广数据列表(export)")
    @GetMapping("export/spread")
    public void exportSpreadData(HttpServletResponse response,
                                 @RequestParam(required = false) String beginTim,
                                 @RequestParam(required = false) String endTime,
                                 @RequestParam(required = false) String curWeek,
                                 @RequestParam(required = false) String curMonth) {
        new ExcelUtil<>(TSpreadData.class)
                .exportExcel(dashboardService
                                .getSpreadDataList(
                                        1,
                                        DEF_EXP_SIZE,
                                        beginTim,
                                        endTime,
                                        curWeek,
                                        curMonth)
                                .getRecords(),
                        "推广数据",
                        response);
    }

    @ApiOperation("财务数据列表(export)")
    @GetMapping("export/finance")
    public void exportFinance(HttpServletResponse response,
                              @RequestParam(required = false) String beginTime,
                              @RequestParam(required = false) String endTime,
                              @RequestParam(required = false) String curWeek,
                              @RequestParam(required = false) String curMonth) {
        new ExcelUtil<>(TFinanceData.class)
                .exportExcel(dashboardService
                                .getFinanceDataList(
                                        1,
                                        DEF_EXP_SIZE,
                                        beginTime,
                                        endTime,
                                        curWeek,
                                        curMonth)
                                .getRecords(),
                        "财务数据",
                        response);
    }


    @ApiOperation("充值/提现记录")
    @PostMapping("balanceLog")
    public ApiResult<PageResult<BalanceLogDTO>> balanceLog(@RequestParam(defaultValue = "1") int page,
                                                           @RequestParam(defaultValue = "10") int size,
                                                           @RequestParam(required = false) @ApiParam("用户名") String nickName,
                                                           @RequestParam(required = false) @ApiParam("id") String id,
                                                           @RequestParam(required = false) @ApiParam("mId") String mId,
                                                           @RequestParam(required = false) @ApiParam("手机号码") String mobile,
                                                           @RequestParam(required = false) @ApiParam("方式 1微信 2支付宝") String method,
                                                           @RequestParam(required = false) @ApiParam("状态") String status,
                                                           @RequestParam(required = false) @ApiParam("开始时间") String addBeginTime,
                                                           @RequestParam(required = false) @ApiParam("结束时间") String addEndTIme,
                                                           @RequestParam(required = false) @ApiParam("真实姓名") String name,
                                                           @RequestParam @ApiParam(" 0消费记录 1充值记录 2提现记录") String tradeType) {
        PageResult<BalanceLogDTO> res =
                dashboardService
                        .balanceLog(page, size, nickName, id, mobile, method, status, addBeginTime, addEndTIme, name, tradeType, mId);
        return ApiResult.success(res);
    }

    @ApiOperation("留存报表列表")
    @PostMapping("preserve")
    public ApiResult<PageResult<TPreserveData>> preserveDataList(@RequestParam(defaultValue = "1", required = false) int page,
                                                                 @RequestParam(defaultValue = "30", required = false) int size,
                                                                 @ApiParam("开始时间") @RequestParam(required = false) String beginTime,
                                                                 @ApiParam("结束时间") @RequestParam(required = false) String endTime,
                                                                 @ApiParam("本周") @RequestParam(required = false) String curWeek,
                                                                 @ApiParam("本月") @RequestParam(required = false) String curMonth) {
        PageResult<TPreserveData> res = dashboardService.getPreserveDataList(page, size, beginTime, endTime, curWeek, curMonth);
        return ApiResult.success(res);
    }

    @ApiOperation("留存报表列表(export)")
    @GetMapping("export/preserve")
    public void exportPreserveData(HttpServletResponse response,
                                   @ApiParam("开始时间") @RequestParam(required = false) String beginTim,
                                   @ApiParam("结束时间") @RequestParam(required = false) String endTime,
                                   @ApiParam("本周") @RequestParam(required = false) String curWeek,
                                   @ApiParam("本月") @RequestParam(required = false) String curMonth) {
        new ExcelUtil<>(TPreserveData.class).exportExcel(dashboardService.getPreserveDataList(1, DEF_EXP_SIZE, beginTim, endTime, curWeek, curMonth).getRecords(), "留存报表", response);
    }

}
